package com.system.xingzhi.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.xingzhi.dao.mapper.ComponentPermissionMapper;
import com.system.xingzhi.model.ComponentPermission;
import com.system.xingzhi.model.ReturnData;
import com.system.xingzhi.service.ComponentPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ComponentPermissionServiceImpl implements ComponentPermissionService {

    @Autowired
    ComponentPermissionMapper componentPermissonMapper;

    @Override
    public ReturnData deleteByPrimaryKey(String componentPermissonId) {
        ReturnData returnData=new ReturnData();
        try {
            componentPermissonMapper.deleteByPrimaryKey(componentPermissonId);
            returnData.setCode(0);
            returnData.setDetail("删除组件权限成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("删除组件权限失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData insert(ComponentPermission record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(componentPermissonMapper.insert(record));
            returnData.setCode(0);
            returnData.setDetail("添加组件权限成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("添加组件权限失败");
            return returnData;
        }
        return returnData;

    }

    @Override
    public int insertSelective(ComponentPermission record) {
        return componentPermissonMapper.insertSelective(record);
    }

    @Override
    public ComponentPermission selectByPrimaryKey(String componentPermissonId) {
        return componentPermissonMapper.selectByPrimaryKey(componentPermissonId);
    }

    @Override
    public int updateByPrimaryKeySelective(ComponentPermission record) {
        return componentPermissonMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ReturnData updateByPrimaryKey(ComponentPermission record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(componentPermissonMapper.updateByPrimaryKey(record));
            returnData.setCode(0);
            returnData.setDetail("更新组件权限组件权限成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("更新组件权限失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData selectByParameter(Map<String, String> map,Integer pageNum, Integer pageSize) {
        ReturnData returnData=new ReturnData();
        try {
            //分页
            PageHelper.startPage(pageNum, pageSize);

            List<ComponentPermission> queryResult = componentPermissonMapper.selectByParameter(map);
            returnData.setData(new PageInfo<>(queryResult));
            returnData.setCode(0);
            returnData.setDetail("查询组件权限成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("查询组件权限失败");
            return returnData;
        }
        return returnData;
    }

}
